<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            width: 100px;
            height: 100px;
            background-color: red;

        }
    </style>
    <script>
        window.onload=function(){
            var div=document.getElementsByTagName('div')[0];
            var btns=document.getElementsByTagName('button');

            btns[0].onclick=function(){
                var id=setTimeout(function(){
                    div.style.marginLeft='200px';
                },2000)
                btns[1].onclick=function(){
                    clearTimeout(id);
                }
            }

            btns[2].onclick=function(){
                var id=setInterval(function(){
                    div.style.marginLeft=div.offsetLeft+20+'px';
                },100)
                btns[3].onclick=function(){
                    clearInterval(id);
                }
            }
            btns[4].onclick=function(){
                function handler(){
                    div.style.marginLeft=div.offsetLeft+20+'px';
                    setTimeout(handler,100);
                }
                setTimeout(handler,100);
            }
           
        }
    </script>
</head>
<body>
    <div></div>
    <button>超时调用</button>
    <button>取消超时</button>
    <button>间歇调用</button>
    <button>取消间歇</button>
    <button>超时模拟间歇</button>
</body>
</html>